package easy.合并两个有序链表;

import easy.合并两个有序链表.ListNode;

import java.util.List;

/**
 * 合并两个有序链表
 *
 * @author codex
 * @since 2021-09-26
 */
public class MergeTwoLists {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
    
    public static void main(String[] args) {
        ListNode l1 = new ListNode();
        l1.val = 1;
        l1.next = new ListNode();
        l1.next.val = 2;
        l1.next.next = new ListNode();
        l1.next.next.val = 4;
    
        ListNode l2 = new ListNode();
        l2.val = 1;
        l2.next = new ListNode();
        l2.next.val = 3;
        l2.next.next = new ListNode();
        l2.next.next.val = 4;
    
        ListNode listNode = mergeTwoLists(l1, l2);
        System.out.println(listNode);
    }
}
